<%@ page import="com.qiku.pojo.User" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>管理员中心</title>
    <style>
        #father {
            width: 100%;
        }

        #title {
            min-width: 1600px;
            min-height: 50px;
            margin: 0 auto;
            background: #8365bdba;
        }
        #yonghu {
            display: block;
            text-align: center;
            font-size: 30px;
            font-weight: bold;

        }
        #wenjian {
            width: 100%;
        }
        #content {
            width: 1600px;
            height: 850px;
            margin: 0 auto;
            background: skyblue;
        }
        .Upload {
            width: 1300px;
            margin: 0 auto;
            border: 3px solid #d4ffd4;
        }
        .pane{
            text-align: center;
            font-size: 20px;
            font-weight: bold;
        }
        #t1,#t2{
            margin: 0 auto;
            text-align: center;
        }
        #a{
            position: absolute;
            top: 20px;
            left: 20px;
        }
        table {
            background-color: rgb(233, 255, 194);
            border: 2px solid rgb(124 209 255);
            color: rgb(195, 82, 82);
            border-radius: 5px;
            font-size: 20px;
        }
        tr {
            background-color: rgba(203, 203, 203, 0.466);
        }
        th {
            background-color: rgb(255, 202, 174);
            border: 2px solid rgb(255, 170, 124);
            color: rgb(82, 106, 195);
            border-radius: 5px;
        }
        td {
            background-color: rgb(255, 202, 174);
            border: 2px solid rgb(255, 170, 124);
            border-radius: 0px;
        }
        a{
            text-decoration: none;
            color: greenyellow;
        }
    </style>
</head>
<%
    String username = (String) session.getAttribute("username");
    User user = (User) session.getAttribute("user");
//    System.out.println("username:"+username);
    Map<String, String> userLoginMap = (Map<String, String>) application.getAttribute("userLoginMap");
    String sid = userLoginMap.get(username);
//    System.out.println("usernameSID:"+sid);
    String newSid = session.getId();
//    System.out.println("usernameNewSID:"+newSid);
    boolean isLogin = username != null && newSid.equals(sid);
    if (!isLogin) {
        response.sendRedirect("login.jsp");
    } else {
%>
<body onload="findResourceList();findAllUser()">
<div id="father">
    <div id="title">
        <span id="yonghu">管理中心</span>
        <a href="main2.jsp" id="a">返回主页</a>
    </div>
</div>

<div id="wenjian">
    <div id="content">
        <div class="Upload">
            <div class="pane">
                用户管理:
                <table border="1" id="t1">
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>注册日期</th>
                        <th>是否管理员</th>
                        <th>正常</th>
                        <th>废弃与恢复</th>
                    </tr>
                    <tr id="fu0">
                        <td  id="sfu0" class="u_id"></td>
                        <td class="u_name"></td>
                        <td class="u_date"></td>
                        <td class="is_root"></td>
                        <td class="is_del"></td>
                        <td>
                            <input class="zhuxiao" type="button" onclick="yonghu(this)" value="" hidden>
                        </td>
                    </tr>
                    <tr id="fu1">
                        <td id="sfu1" class="u_id"></td>
                        <td class="u_name"></td>
                        <td class="u_date"></td>
                        <td class="is_root"></td>
                        <td class="is_del"></td>
                        <td>
                            <input class="zhuxiao" type="button" onclick="yonghu(this)" value="" hidden>
                        </td>
                    </tr>
                    <tr id="fu2">
                        <td id="sfu2" class="u_id"></td>
                        <td class="u_name"></td>
                        <td class="u_date"></td>
                        <td class="is_root"></td>
                        <td class="is_del"></td>
                        <td>
                            <input class="zhuxiao" type="button" onclick="yonghu(this)" value="" hidden>
                        </td>
                    </tr>
                    <tr id="fu3">
                        <td id="sfu3" class="u_id"></td>
                        <td class="u_name"></td>
                        <td class="u_date"></td>
                        <td class="is_root"></td>
                        <td class="is_del"></td>
                        <td>
                            <input class="zhuxiao" type="button" onclick="yonghu(this)" value="" hidden>
                        </td>
                    </tr>
                    <tr id="fu4">
                        <td id="sfu4" class="u_id"></td>
                        <td class="u_name"></td>
                        <td class="u_date"></td>
                        <td class="is_root"></td>
                        <td class="is_del"></td>
                        <td>
                            <input class="zhuxiao" type="button" onclick="yonghu(this)" value="" hidden>
                        </td>
                    </tr>
                    <tr id="fu5">
                        <td id="sfu5" class="u_id"></td>
                        <td class="u_name"></td>
                        <td class="u_date"></td>
                        <td class="is_root"></td>
                        <td class="is_del"></td>
                        <td>
                            <input class="zhuxiao" type="button" onclick="yonghu(this)" value="" hidden>
                        </td>
                    </tr>
                </table>
                <button id="shang1" onclick="fanye1(-1)">上一页</button>
                <span id="dangqianye1">1</span>
                /<span id="zongye1">1</span>
                <button id="xia1" onclick="fanye1(+1)">下一页</button>
            </div>
        </div>
        <br>
        <br>
        <div class="Upload">
            <div class="pane">
                文件资源管理:
                <table border="1" id="t2">
                    <tr>
                        <th>文件名</th>
                        <th>上传用户</th>
                        <th>上传时间</th>
                        <th>文件类型</th>
                        <th>公开与隐藏</th>
                        <th>状态切换</th>
                    </tr>
                    <tr id="ffu0">
                        <td>
                            <input id="sffu0" class="ziyuanID" type='text' value="" hidden>
                            <a class="r_name" href="" ></a>
                        </td>
                        <td class="up_ming">-</td>
                        <td class="up_date">-</td>
                        <td class="up_type">-</td>
                        <td id="ssffu0" class="is_xianshi">-</td>
                        <td>
                            <input class="qiehuan" type="button" onclick="qiehuan(this)" value="切换" hidden>
                        </td>
                    </tr>
                    <tr id="ffu1">
                        <td>
                            <input id="sffu1" class="ziyuanID" type='text' value="" hidden>
                            <a class="r_name" href="" ></a>
                        </td>
                        <td class="up_ming">-</td>
                        <td class="up_date">-</td>
                        <td class="up_type">-</td>
                        <td id="ssffu1" class="is_xianshi">-</td>
                        <td>
                            <input class="qiehuan" type="button" onclick="qiehuan(this)" value="切换" hidden>
                        </td>
                    </tr>
                    <tr id="ffu2">
                        <td>
                            <input id="sffu2" class="ziyuanID" type='text' value="" hidden>
                            <a class="r_name" href="" ></a>
                        </td>
                        <td class="up_ming">-</td>
                        <td class="up_date">-</td>
                        <td class="up_type">-</td>
                        <td id="ssffu2" class="is_xianshi">-</td>
                        <td>
                            <input class="qiehuan" type="button" onclick="qiehuan(this)" value="切换" hidden>
                        </td>
                    </tr>
                    <tr id="ffu3">
                        <td>
                            <input id="sffu3" class="ziyuanID" type='text' value="" hidden>
                            <a class="r_name" href="" ></a>
                        </td>
                        <td class="up_ming">-</td>
                        <td class="up_date">-</td>
                        <td class="up_type">-</td>
                        <td id="ssffu3" class="is_xianshi">-</td>
                        <td>
                            <input class="qiehuan" type="button" onclick="qiehuan(this)" value="切换" hidden>
                        </td>
                    </tr>
                    <tr id="ffu4">
                        <td>
                            <input id="sffu4" class="ziyuanID" type='text' value="" hidden>
                            <a class="r_name" href="" ></a>
                        </td>
                        <td class="up_ming">-</td>
                        <td class="up_date">-</td>
                        <td class="up_type">-</td>
                        <td id="ssffu4" class="is_xianshi">-</td>
                        <td>
                            <input class="qiehuan" type="button" onclick="qiehuan(this)" value="切换" hidden>
                        </td>
                    </tr>
                    <tr id="ffu5">
                        <td>
                            <input id="sffu5" class="ziyuanID" type='text' value="" hidden>
                            <a class="r_name" href="" ></a>
                        </td>
                        <td class="up_ming">-</td>
                        <td class="up_date">-</td>
                        <td class="up_type">-</td>
                        <td id="ssffu5" class="is_xianshi">-</td>
                        <td>
                            <input class="qiehuan" type="button" onclick="qiehuan(this)" value="切换" hidden>
                        </td>
                    </tr>
                </table>
                <button id="shang" onclick="fanye(-1)">上一页</button>
                <span id="dangqianye">1</span>
                /<span id="zongye">1</span>
                <button id="xia" onclick="fanye(+1)">下一页</button>
            </div>
        </div>
    </div>
</div>
<%
    }
%>
</body>
<script src="./js/jquery-3.6.0.js"></script>
<script>

    //资源状态切换
    function qiehuan(l){
        var lNode = l.parentNode.parentNode;
        var slId = "s" + lNode.getAttribute("id");
        var rId = $("#" + slId).text();
        var sslId = "ss" + lNode.getAttribute("id");
        var zhuangtai = $("#" + sslId).text();
        console.log("id："+ rId)
        var med = "";
        if (zhuangtai == "公开"){
            med = "deleteResource";
        }else {
            med = "recoverResource";
        }
        $.ajax({
            url:"resource",
            data:{
                "methodName":med,
                "resource_id":rId
            },
            type:"post",
            dataType:"json",
            success:function (data){
                alert(data.deleteMsg);
                findResourceList();
            },
            error:function (){
                alert("发生了一个错误！");
            }
        });

    }
    //用户的操作函数
    function yonghu(l){
        var val = l.value;
        var lNode = l.parentNode.parentNode
        var zId = "s" + lNode.getAttribute("id");
        var uId = $("#" + zId).text();
        var med = "";
        if (val == "恢复"){
            med = "recoverUser";
        }else{
            med = "destroyUser";
        }
        $.ajax({
            url:"user",
            data:{
                "methodName":med,
                "id":uId
            },
            type:"post",
            dataType:"json",
            success:function (data){
                alert(data.destroyMsg);
                findAllUser();
            },
            error:function (){
                alert("发生了一个错误！");
            }
        });
    }

    //实现翻页的函数
    function fanye(num) {
        //当前页
        var ye = parseInt($("#dangqianye").text());
        //总页
        var zongye = parseInt($("#zongye").text());
        switch (num) {
            case 1:
                if (ye < zongye) {
                    ye = ye + num;
                }
                break;
            case -1:
                if (ye > 1) {
                    ye = ye + num;
                }
                break;
        }
        $("#dangqianye").text(ye);
        findResourceList();
    }

    //实现翻页的函数1
    function fanye1(num) {
        //当前页
        var ye1 = parseInt($("#dangqianye1").text());
        //总页
        var zongye1 = parseInt($("#zongye1").text());
        switch (num) {
            case 1:
                if (ye1 < zongye1) {
                    ye1 = ye1 + num;
                }
                break;
            case -1:
                if (ye1 > 1) {
                    ye1 = ye1 + num;
                }
                break;
        }
        $("#dangqianye1").text(ye1);
        findResourceList();
    }

    //刷新数据函数
    function findResourceList(){
        $.ajax({
            url:"resource",
            data:{
                "methodName":"findResourceList",
                "is_num":0
            },
            type:"get",
            dataType:"json",
            success:function (data){
                console.log(data)
                var $ziyuanID = $(".ziyuanID");
                var $rName = $(".r_name");
                var $upMing = $(".up_ming");
                var $upDate = $(".up_date");
                var $upType = $(".up_type");
                var $isXianshi = $(".is_xianshi");
                var $qiehuan = $(".qiehuan");
                var length = data.length;
                for (var i = 0; i < 6; i++){
                    $ziyuanID.eq(i).text("");
                    $rName.eq(i).attr("href","");
                    $rName.eq(i).text("");
                    $rName.eq(i).attr("style", "");
                    $rName.eq(i).attr("download", "");
                    $upMing.eq(i).text("-");
                    $upDate.eq(i).text("-");
                    $upType.eq(i).text("-");
                    $isXianshi.eq(i).text("-");
                    $qiehuan.eq(i).attr("hidden",true);
                }

                var ye = $("#dangqianye").text();
                var dangqian = parseInt(ye);
                var length = data.length;
                var yeshu = (length % 6 == 0 ? length / 6 : parseInt(length / 6) + 1)
                $("#zongye").text(yeshu);
                var num = dangqian * 6 >= length ? length - (dangqian * 6 - 6) : dangqian * 6
                for (var i = dangqian * 6 - 6; i < dangqian * 6 - 6 + num; i++) {
                    var r = data[i];
                    var resourceId = r.resource_id;
                    var resourceName = r.resource_name;
                    var uploaderName = r.uploader_name;
                    var uploadDate = r.upload_date;
                    var type = r.type;
                    var audit = (r.is_audit) == 1 ? "公开":"隐藏";
                    var fileUrl = r.file_url;
                    //控件赋值
                    var ii = i % 6;
                    $ziyuanID.eq(ii).text(resourceId);
                    $rName.eq(ii).attr("href","http://" + fileUrl);
                    $rName.eq(ii).text(resourceName);
                    $rName.eq(ii).attr("style", "color:rgb(173,90,204);");
                    $rName.eq(ii).attr("download", resourceId + "_" + resourceName);
                    $upMing.eq(ii).text(uploaderName);
                    $upDate.eq(ii).text(uploadDate.substring(0,11));
                    $upType.eq(ii).text(type);
                    $isXianshi.eq(ii).text(audit);
                    $qiehuan.eq(ii).attr("hidden",false);
                }
            },
            error:function (){
                console.log("失败")
            }
        });
    }

    //刷新数据1
    function findAllUser(){
        $.ajax({
            url:"user",
            data:{
                "methodName":"findAllUser"
            },
            type: "get",
            dataType: "json",
            success:function (data){
                var $uId = $(".u_id");
                var $uName = $(".u_name");
                var $uDate = $(".u_date");
                var $isRoot = $(".is_root");
                var $isDel = $(".is_del");
                var $zhuxiao = $(".zhuxiao");
                var userID = <%=user.getId()%>;
                for (var i = 0; i < 6; i++){
                    $uId.eq(i).text("-")
                    $uName.eq(i).text("-")
                    $uDate.eq(i).text("-")
                    $isRoot.eq(i).text("-")
                    $isDel.eq(i).text("-")
                    $zhuxiao.eq(i).attr("hidden",true);
                    $zhuxiao.eq(i).val("");
                }
                var user_id = ${user.getId()};
                var data1 = new Array();
                var index = 0;
                for (var x = 0; x < data.length; x++){
                    var datum = data[x];
                    var id = datum.id;
                    if (id != user_id) {
                        data1[index] = datum;
                        index++;
                    }
                }
                var length1 = data1.length;

                var ye1 = $("#dangqianye1").text();
                var dangqian1 = parseInt(ye1);
                var yeshu1 = (length1 % 6 == 0 ? length1 / 6 : parseInt(length1 / 6) + 1)
                $("#zongye1").text(yeshu1);
                var num1 = dangqian1 * 6 >= length1 ? length1 - (dangqian1 * 6 - 6) : dangqian1 * 6
                for (var i = dangqian1 * 6 - 6; i < dangqian1 * 6 - 6 + num1; i++) {
                    var r = data1[i];
                    var ii = i % 6;
                    var uId = r.id;
                    var uName = r.username;
                    var uDate = r.register_date;
                    var root = (r.is_root) == 1 ? "管理员":"用户";
                    var del = r.is_del;
                    var delText = "";
                    var text = "";
                    if (del == 1){
                        delText = "已废弃";
                        text = "恢复";
                    }else{
                        delText = "正常";
                        text = "废弃";
                    }
                    //控件赋值
                    $uId.eq(ii).text(uId)
                    $uName.eq(ii).text(uName)
                    $uDate.eq(ii).text(uDate)
                    $isRoot.eq(ii).text(root)
                    $isDel.eq(ii).text(delText)
                    $zhuxiao.eq(ii).val(text);
                    $zhuxiao.eq(ii).attr("hidden",false);
                }
            },
            error:function (){
                console.log("错误")
            }
        });
    }
</script>
</html>